<?php

	class cmd_memberPoint extends mdl_memberPoint
	{
	    //++++++++++所得积分
		function payAllGetPoint($userId, $orderId)
		{
			$oPointHistory = $this->system->loadModel('trading/pointHistory');
			$oOrder = $this->system->loadModel('trading/order');
			$aTmp = $oOrder->getFieldById($orderId, array('score_g'));
			$nPayPoint = $aTmp['score_g'];
			$this->sharePointByPromoter($userId,$nPayPoint,'promotion_link_get',$orderId);		//推广人享受被推广人的积分
			return $this->chgPoint($userId, $nPayPoint, 'order_pay_get', $orderId);
		}

		function sharePointForPromoter($userId,$nPoint,$sReason,$relatedId)
		{
			if ($nPoint>0)
			{
				/* alone add start 2009年11月11号 17:38:39 */
				$oMem = $this->system->loadModel('member/member');
				$aMem = $oMem->getFieldById($userId, array('refer_id'));		//根据会员ID取得refer_id
				$oOrder = $this->system->loadModel('trading/order');
				$aOrder = $oOrder->getFieldById($relatedId, array('refer_id'));		//根据订单ID取得refer_id
				//先根据订单的refer_id取得推广链接的会员名
				if (strstr($aOrder['refer_id'],'p'))
				{
					$uname = substr($aOrder['refer_id'],1);
					$mem = $oMem->getMemIdByName($uname);
					if ($mem)
					{
						$userId = $mem[0]['member_id'];		//推广链接的会员ID
					}
				}
				//如果订单表中refer_id不存在,则从会员表中找到refer_id
				elseif (strstr($aMem['refer_id'],'p'))
				{
					$uname = substr($aMem['refer_id'],1);
					$mem = $oMem->getMemIdByName($uname);
					if ($mem)
					{
						$userId = $mem[0]['member_id'];		//推广链接的会员ID
					}
				}
				//如果订单和会员表中refer_id都不存在 返回true
				else return true;

				if ($userId)
				{
					/*
					if ($nPoint==0){
						return true;
					}else if(!$this->_chgPoint($userId, $nPoint)) {
						trigger_error(__('积分扣除超过会员已有积分'), E_USER_ERROR);
						return false;
					}
					*/
					$aPoint = $oMem->getFieldById($userId, array('point'));
					$oLv = $this->system->loadModel('member/level');
					if ($userId && ($oLv->checkMemLvType($userId) != 'wholesale'))
					{
						$userId = intval($userId);
						$oPointHistory = $this->system->loadModel('trading/pointHistory');
						$aUserPoint['point'] = $this->getMemberPoint($userId);
						$aUserPoint['point'] += $nPoint;

						$rRs = $this->db->query('select * from sdb_members where member_id='.$userId);
						$sSql = $this->db->GetUpdateSQL($rRs, $aUserPoint);
						if($sSql)$this->db->exec($sSql);
						$this->toUpdatelevel($userId);
						$aPointHistory = array(
											'member_id' => $userId,
											'point' => $nPoint,
											'reason' => $sReason,
											'related_id' => $relatedId);
						$oPointHistory->addHistory($aPointHistory);
					}
				}
			}
			return true;
		}
	}
?>